package com.yc.cloudvideo.sp_user.controller;


import com.yc.cloudvideo.entity.User;
import com.yc.cloudvideo.sp_user.mapper.UserMapper;
import com.yc.cloudvideo.sp_user.service.impl.UserServiceImpl;
import com.yc.common.bean.Result;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author LYYT
 * @since 2022-08-03
 */
@RestController
@RequestMapping("/cloudvideo/user")
public class UserController {

    /**
     * 注册账号
     */
    @Resource
    private UserServiceImpl userService;

    @PostMapping("create")
    public Map<String, Object> createAccount(User user) {
        return userService.createAccount(user);
    }

    @PostMapping("log")
    public Map<String, Object> loginAccount(User user, HttpSession session) {
        return userService.loginAccount(user, session);
    }

    /**
     * 激活账号
     *
     * @param confirmCode
     * @return
     */
    @GetMapping("activation")
    public Map<String, Object> activationAccount(String confirmCode) {
        return userService.activationAccount(confirmCode);
    }

    @RequestMapping("logout")
    public Result logout(HttpSession session) {
        //会话失效
        session.invalidate();
        return new Result(1, "退出成功", null);
    }

    //http://127.0.0.1:8003/cloudvideo/user/getLoginedUser
    @GetMapping("getLoginedUser")
    public Result getLoginedUser(HttpSession session) {
        Object loginedUser = session.getAttribute("loginedUser");
        System.out.println("loginedUser=" + loginedUser);
        if (loginedUser == null) {
            return new Result(0, "用户未登录", null);
        } else {
            return new Result(1, "用户已登录", loginedUser);
        }
/**
 * loginedUser=[User{uid=null, headImage=null, nickName=null, sign=null,
 * email=1052772120@qq.com, password=ba975662d297f6da4e29eef638d87517, isBoy=null, age=null,
 * regTime=null, exist=null, banTime=null, root=null, vip=null}]
 */

    }


    /**
     * ======================================================================
     */
    @Resource
    UserMapper userMapper;

    //获取仓库拥有者信息
    @PostMapping("selectStoreOwner")
    User selectStoreOwner(@RequestParam("cid") int cid) {
        return userMapper.selectStoreOwner(cid);
    }

    //获取仓库贡献者信息
    @PostMapping("getUsersGByCid")
    List<User> getUsersGByCid(int cid) {
        List<User> users = userMapper.selectUserGByCid(cid);
        return users;
    }

    //获取用户信息
    @PostMapping("getUserByUid")
    @ResponseBody
    User getUsersGByUid(int uid){
        User user = userMapper.selectUserByUid(uid);
        return user;
    }

}

